﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcWesbNews.Models;

namespace MvcWesbNews.Controllers
{ 
    public class DetailNewsController : Controller
    {
        private MyNewsEntities db = new MyNewsEntities();

        //
        // GET: /DetailNews/

        public ViewResult Index()
        {
            var detailnews = db.DetailNews.Include("GroupNew");
            return View(detailnews.ToList());
        }

        //
        // GET: /DetailNews/Details/5

        public ViewResult Details(int id)
        {
            DetailNew detailnew = db.DetailNews.Single(d => d.NewsID == id);
            return View(detailnew);
        }

        //
        // GET: /DetailNews/Create

        public ActionResult Create()
        {
            ViewBag.GroupID = new SelectList(db.GroupNews, "GroupID", "Caption");
            return View();
        } 

        //
        // POST: /DetailNews/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(DetailNew detailnew)
        {
            if (ModelState.IsValid)
            {
                db.DetailNews.AddObject(detailnew);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.GroupID = new SelectList(db.GroupNews, "GroupID", "Caption", detailnew.GroupID);
            return View(detailnew);
        }
        
        //
        // GET: /DetailNews/Edit/5
        [ValidateInput(false)]
        public ActionResult Edit(int id)
        {
            DetailNew detailnew = db.DetailNews.Single(d => d.NewsID == id);
            ViewBag.GroupID = new SelectList(db.GroupNews, "GroupID", "Caption", detailnew.GroupID);
            return View(detailnew);
        }

        //
        // POST: /DetailNews/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(DetailNew detailnew)
        {
            if (ModelState.IsValid)
            {
                db.DetailNews.Attach(detailnew);
                db.ObjectStateManager.ChangeObjectState(detailnew, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GroupID = new SelectList(db.GroupNews, "GroupID", "Caption", detailnew.GroupID);
            return View(detailnew);
        }

        //
        // GET: /DetailNews/Delete/5
 
        public ActionResult Delete(int id)
        {
            DetailNew detailnew = db.DetailNews.Single(d => d.NewsID == id);
            return View(detailnew);
        }

        //
        // POST: /DetailNews/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            DetailNew detailnew = db.DetailNews.Single(d => d.NewsID == id);
            db.DetailNews.DeleteObject(detailnew);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}